﻿<!-- 主体部分 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">字典查询:</label>
                        <div class="layui-input-inline">
                            <input name="DictValue" class="layui-input" placeholder="输入字典名称" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        &emsp;
                        <button class="layui-btn icon-btn" lay-filter="TbSearch2" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="gridTable2" lay-filter="gridTable2"></table>
        </div>
    </div>
</div>

<!-- js部分 -->
<script>
    // 全局变量
    var table;

    layui.use(['layer', 'form', 'table', 'util', 'admin', 'setter'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var setter = layui.setter;

        // 表格初始化
        layuiTableSet();

        /* 渲染表格 */
        var insTb2 = table.render({
            elem: '#gridTable2',
            url: setter.baseServer + '/SystemManage/DataDictDetail/GetListJson?DictType=' + checkId,
            page: false,
            toolbar: "#toolbar2",
            height: 'full-100',
            size: 'sm',
            cols: [[
                { type: 'checkbox' },
                { type: 'numbers' },
                { field: 'DictKey', title: '字典键', width: 85, sort: true },
                { field: 'DictValue', title: '字典项名称' },
                { field: 'DictSort', title: '排序号', sort: true, align: 'center', width: 85 },
                {
                    field: 'IsDefault', title: '是否默认', width: 85, align: 'center', templet: function (d) {
                        if (d.IsDefault == "1") {
                            return '<span class="layui-badge layui-badge-blue">是</span>';
                        } else {
                            return '<span class="layui-badge layui-badge-green">否</span>';
                        }
                    }
                },
                {
                    field: 'DictStatus', title: '是否启用', width: 85, align: 'center', templet: function (d) {
                        if (d.DictStatus == "1") {
                            return '<span class="layui-badge layui-badge-blue">启用</span>';
                        } else {
                            return '<span class="layui-badge layui-badge-red">禁用</span>';
                        }
                    }
                },
                {
                    field: 'CreateTime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.CreateTime);
                    }, sort: true
                }
            ]]
        });
        /* 表格搜索 */
        form.on('submit(TbSearch2)', function (data) {
            insTb2.reload({ where: data.field, page: { curr: 1 } });
            return false;
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(gridTable2)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'edit') { // 修改
                var checkRows = table.checkStatus('gridTable2');
                if (checkRows.data.length === 0 || checkRows.data.length > 1) {
                    layer.msg('请选择且只选择一条数据', { icon: 2 });
                    return;
                }
                showEditModel(checkRows.data[0]);
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('gridTable2');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', { icon: 2 });
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.Id;
                });
                doDel({ ids: ids });
            } else if (obj.event == 'export') {
                // 导出
                var cols = [[
                    { field: 'Remark', title: '字典描述', width: 300 },
                    { field: 'DictType', title: '字典类型' },
                    {
                        field: 'CreateTime', title: '创建时间', templet: function (d) {
                            return util.toDateString(d.CreateTime);
                        }
                    },
                ]];
                layuiTableExport(cols, setter.baseServer + '/SystemManage/DataDictDetail/GetListJson', '字典键："' + checkId + '"导出')
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            var id = 0;
            if (mData != undefined && mData != null) { id = mData.Id; }
            admin.open({
                type: 2,
                area: ['650px', '500px'],
                title: (mData ? '修改' : '添加'),
                content: 'datadictdetailForm.html?id=' + id + '&DictType=' + checkId,
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/SystemManage/DataDictDetail/DeleteFormJson', {
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (1 === res.Tag) {
                        layer.msg("操作成功", { icon: 1 });
                        insTb2.reload({ page: { curr: 1 } });
                    } else {
                        layer.msg(res.Message, { icon: 2 });
                    }
                }, 'post');
            });
        }

    });
</script>